package com.dhj.optionalTest;

import com.dhj.streamTest.entity.Author;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

public class Demo3 {

    public static void main(String[] args) {
        Author author = getAuthor();
        Optional<Author> optional = Optional.ofNullable(author);
        System.out.println(optional.get().getName());

        System.out.println("----------------------------");

        Author author2 = getAuthor2();
        Optional<Author> optional2 = Optional.ofNullable(author2);
        try{
            System.out.println(optional2.get().getName());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }


        System.out.println("----------------------------");

        Author author3 = optional2.orElseGet(()->getAuthor());
        System.out.println(author3.getName());

        System.out.println("----------------------------");

        try{
            Author au2 = optional2.orElseThrow(() -> new RuntimeException("数据为null"));
            System.out.println(au2.getName());
        }catch (RuntimeException e){
            System.out.println(e.getMessage());
        }


    }


    public static Author getAuthor() {
        Author author = new Author(5L, "林则徐1", 45, "苟利国家生死以", null);
        return author;
    }


    public static Author getAuthor2() {
        return null;
    }

}
